package com.chance.cc.crawler.prod.command.job.domain.vm.dcdapp.realtime;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.CrawlerJob;
import com.chance.cc.crawler.core.downloader.HttpConfig;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.filter.FilterUtils;
import com.chance.cc.crawler.core.record.CrawlerRecord;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant;
import com.chance.cc.crawler.meta.core.bean.job.CrawlerScheduleJob;
import com.chance.cc.crawler.prod.command.job.domain.vm.dcdapp.DCDAppCommonCrawlerSchedulerJob;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRecordFilter.dateRange;
import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRequestType.*;
import static com.chance.cc.crawler.development.scripts.allfeild.AICCommonField.Tag_Site_Info;

/**
 * @ClassName dcd
 * @Description TODO
 * @Author songding
 * @Date 2021/9/24 16:50
 * @Version 1.0
 **/
public class DcdAppEveryDayRealtimeCrawlerScheduleJob extends DCDAppCommonCrawlerSchedulerJob {
    private static final String DOMAIN = "dcdapp";
    private static final String crawler_level = "realtime";

    public static void main(String[] args) {
//        publishCrawlerScheduleJobInfo();
        System.out.println(crawlerSchedulejobWenda().generateCrawlerJobKey());
    }

    public static CrawlerJob publishCrawlerScheduleJobInfo(){
        //CrawlerJob crawlerJob = crawlerSchedulejobWenda();//问答
       // CrawlerJob crawlerJob = crawlerSchedulejobDongtai();//动态
        // CrawlerJob crawlerJob = crawlerSchedulejobjinghua();//精华
        CrawlerJob crawlerJob = crawlerSchedulejobPraise();//口碑
        //发布定时采集作业
        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();
        crawlerScheduleJob.setDomain(domain);
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(crawlerJob));
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());
        crawlerScheduleJob.setNote("懂车帝四板块每日采集");
        crawlerScheduleJob.setCrawlerKey(crawlerJob.generateCrawlerKey());
        HttpPage page = metaServiceCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);
        System.out.println("发布crawler作业：" + page.getRawText());
        return crawlerJob;
    }
    //问答
    public static CrawlerJob crawlerSchedulejobWenda(){
        String url = "https://www.dongchedi.com/";
        String scriptSite = "dynamic";
        String site = "realtime";
        String site_biz = "wenda";
        String siteInfo = "series-everyDay";

        CrawlerRequestRecord crawlerRequestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(DOMAIN, CrawlerEnum.CrawlerRequestType.turnPage)
                .domain(DOMAIN)
                .httpUrl(url)
                .httpConfig(HttpConfig.me(DOMAIN))
                .filter(CrawlerEnum.CrawlerRecordFilter.dateRange)
                .addFilterInfo(FilterUtils.dateRangeFilterInfo(24*7,null))
                .releaseTime(System.currentTimeMillis())
                .resultLabelTag(CrawlerEnum.CrawlerDataType.article)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.interaction)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.comment)
                .build();
        crawlerRequestRecord.tagsCreator().bizTags().addDomain(DOMAIN);
        crawlerRequestRecord.tagsCreator().bizTags().addSite(scriptSite);
        crawlerRequestRecord.tagsCreator().bizTags().addSiteBiz(site_biz);
        crawlerRequestRecord.tagsCreator().bizTags().addCustomKV(Tag_Site_Info,siteInfo);
        crawlerRequestRecord.tagsCreator().bizTags().addCustomKV("domainName","dongchedi");

        //创建 评论信息的过滤条件
        CrawlerRecord filterCrawlerRecord =  new CrawlerRecord();
        filterCrawlerRecord.setFilter(CrawlerEnum.CrawlerRecordFilter.dateRange);
        filterCrawlerRecord.addFilterInfo(FilterUtils.dateRangeFilterInfo(24*1,null));
        crawlerRequestRecord.tagsCreator().bizTags().addCustomKV("comment_record_filter_info", JSON.toJSONString(filterCrawlerRecord));
        //添加车友圈的参数
        wenda(crawlerRequestRecord);

        CrawlerRequestRecord keywordRecord = CrawlerRequestRecord.builder()
                .startPageRequest("dcdapp_series_keyword",turnPageItem)
                .httpUrl("http://192.168.1.217:9599/v1/meta/dcdapp/keys?site=series")
                .requestLabelTag(supportSource)
                .requestLabelTag(internalDownload)
                .build();

        return CrawlerJob.builder()
                .crawlerJobThreadNumber(10)
                .triggerInfo(
                        domain,
                        CrawlerMetaConstant.ScheduleJobTrigger_Simple,//                            dcdapp-dynamic-everyday-realtime-wenda-everyday-realtime-simple
                        System.currentTimeMillis(),//dcdapp-realtime-wenda-series-everyDay-realtime dcdapp-dynamic-everyDay-realtime-wenda-everyDay-realtime
                        StringUtils.joinWith("-",site,site_biz,siteInfo,CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime))//crawler-dcdapp-forum-forum-realtime-queue
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(StringUtils.joinWith("-","crawler",domain,site,site_biz,"queue")))
                //.fileResultPipeline("kafka","/data/chance_crawler_runner/logs/node/dcdapp.log",false)
                .kafkaResultPipeline("kafka",kafkaTopicForRealTimeJob,null)
                .requestRecord(crawlerRequestRecord)
                .supportRecord(keywordRecord)
                .build();
    }
    public static CrawlerJob crawlerSchedulejobDongtai(){
        String url = "https://www.dongchedi.com/";
        String scriptSite = "dynamic";
        String site = "realtime";
        String site_biz = "dongtai";
        String siteInfo = "series-everyDay";
        CrawlerRequestRecord crawlerRequestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(DOMAIN, CrawlerEnum.CrawlerRequestType.turnPage)
                .domain(DOMAIN)
                .httpUrl(url)
                .httpConfig(HttpConfig.me(DOMAIN))
                .filter(CrawlerEnum.CrawlerRecordFilter.dateRange)
                .addFilterInfo(FilterUtils.dateRangeFilterInfo(24*7,null))
                .releaseTime(System.currentTimeMillis())
                .resultLabelTag(CrawlerEnum.CrawlerDataType.article)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.interaction)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.comment)
                .build();
        crawlerRequestRecord.tagsCreator().bizTags().addDomain(DOMAIN);
        crawlerRequestRecord.tagsCreator().bizTags().addSite(scriptSite);
        crawlerRequestRecord.tagsCreator().bizTags().addSiteBiz(site_biz);
        crawlerRequestRecord.tagsCreator().bizTags().addCustomKV(Tag_Site_Info,siteInfo);
        crawlerRequestRecord.tagsCreator().bizTags().addCustomKV("domainName","dongchedi");

        //创建 评论信息的过滤条件
        CrawlerRecord filterCrawlerRecord =  new CrawlerRecord();
        filterCrawlerRecord.setFilter(CrawlerEnum.CrawlerRecordFilter.dateRange);
        filterCrawlerRecord.addFilterInfo(FilterUtils.dateRangeFilterInfo(24*1,null));
        crawlerRequestRecord.tagsCreator().bizTags().addCustomKV("comment_record_filter_info", JSON.toJSONString(filterCrawlerRecord));
        //添加车友圈的参数
        dongtai(crawlerRequestRecord);

        CrawlerRequestRecord keywordRecord = CrawlerRequestRecord.builder()
                .startPageRequest("dcdapp_series_keyword",turnPageItem)
                .httpUrl("http://192.168.1.217:9599/v1/meta/dcdapp/keys?site=series")
                .requestLabelTag(supportSource)
                .requestLabelTag(internalDownload)
                .build();

        return CrawlerJob.builder()
                .crawlerJobThreadNumber(10)
                .triggerInfo(
                        domain,
                        CrawlerMetaConstant.ScheduleJobTrigger_Simple,
                        System.currentTimeMillis(),//dcdapp-realtime-dongtai-series-everyDay-realtime
                        StringUtils.joinWith("-",site,site_biz,siteInfo,CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime))//crawler-dcdapp-forum-forum-realtime-queue
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(StringUtils.joinWith("-","crawler",domain,site,site_biz,"queue")))
                .kafkaResultPipeline("kafka",kafkaTopicForRealTimeJob,null)
                .requestRecord(crawlerRequestRecord)
                .supportRecord(keywordRecord)
                .build();
    }
    public static CrawlerJob crawlerSchedulejobjinghua(){
        String url = "https://www.dongchedi.com/";
        String scriptSite = "dynamic";
        String site = "realtime";
        String site_biz = "jinghua";
        String siteInfo = "series-everyDay";
        CrawlerRequestRecord crawlerRequestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(DOMAIN, CrawlerEnum.CrawlerRequestType.turnPage)
                .domain(DOMAIN)
                .httpUrl(url)
                .httpConfig(HttpConfig.me(DOMAIN))
                .filter(CrawlerEnum.CrawlerRecordFilter.dateRange)
                .addFilterInfo(FilterUtils.dateRangeFilterInfo(24*7,null))
                .releaseTime(System.currentTimeMillis())
                .resultLabelTag(CrawlerEnum.CrawlerDataType.article)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.interaction)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.comment)
                .build();
        crawlerRequestRecord.tagsCreator().bizTags().addDomain(DOMAIN);
        crawlerRequestRecord.tagsCreator().bizTags().addSite(scriptSite);
        crawlerRequestRecord.tagsCreator().bizTags().addSiteBiz(site_biz);
        crawlerRequestRecord.tagsCreator().bizTags().addCustomKV(Tag_Site_Info,siteInfo);
        crawlerRequestRecord.tagsCreator().bizTags().addCustomKV("domainName","dongchedi");

        //创建 评论信息的过滤条件
        CrawlerRecord filterCrawlerRecord =  new CrawlerRecord();
        filterCrawlerRecord.setFilter(CrawlerEnum.CrawlerRecordFilter.dateRange);
        filterCrawlerRecord.addFilterInfo(FilterUtils.dateRangeFilterInfo(24*1,null));
        crawlerRequestRecord.tagsCreator().bizTags().addCustomKV("comment_record_filter_info", JSON.toJSONString(filterCrawlerRecord));
        //添加车友圈的参数
        jinghua(crawlerRequestRecord);

        CrawlerRequestRecord keywordRecord = CrawlerRequestRecord.builder()
                .startPageRequest("dcdapp_series_keyword",turnPageItem)
                .httpUrl("http://192.168.1.217:9599/v1/meta/dcdapp/keys?site=series")
                .requestLabelTag(supportSource)
                .requestLabelTag(internalDownload)
                .build();

        return CrawlerJob.builder()
                .crawlerJobThreadNumber(10)
                .triggerInfo(
                        domain,
                        CrawlerMetaConstant.ScheduleJobTrigger_Simple,
                        System.currentTimeMillis(),//dcdapp-realtime-jinghua-series-everyDay-realtime
                        StringUtils.joinWith("-",site,site_biz,siteInfo,CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime))//crawler-dcdapp-forum-forum-realtime-queue
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(StringUtils.joinWith("-","crawler",domain,site,site_biz,"queue")))
                .kafkaResultPipeline("kafka",kafkaTopicForRealTimeJob,null)
                .requestRecord(crawlerRequestRecord)
                .supportRecord(keywordRecord)
                .build();
    }

    public static CrawlerJob crawlerSchedulejobPraise(){
        String url = "https://www.dcdapp.com/";
        String site = "praise";
        String site_biz = "koubei";
        String site_Info = "simple-everyDay";
        CrawlerRequestRecord crawlerRequestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domain, turnPage)
                .domain(domain)
                .httpUrl(url)
                .httpConfig(HttpConfig.me(domain))
                .filter(CrawlerEnum.CrawlerRecordFilter.dateRange)
                .addFilterInfo(FilterUtils.dateRangeFilterInfo(24 *7,null))
                .releaseTime(System.currentTimeMillis())
                .resultLabelTag(CrawlerEnum.CrawlerDataType.article)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.interaction)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.comment)
                .build();
        crawlerRequestRecord.tagsCreator().bizTags().addDomain(domain);
        crawlerRequestRecord.tagsCreator().bizTags().addSite(site);
        crawlerRequestRecord.tagsCreator().bizTags().addSiteBiz(site_biz);
        crawlerRequestRecord.tagsCreator().bizTags().addCustomKV(Tag_Site_Info,site_Info);

        CrawlerRecord commentFilter = new CrawlerRequestRecord();
        commentFilter.setFilter(dateRange);
        commentFilter.addFilterInfo(FilterUtils.dateRangeFilterInfo(24*7,null));
        crawlerRequestRecord.tagsCreator().resultTags().getCategoryTag().addKVTag("comment_filter_info", JSON.toJSONString(commentFilter));

        CrawlerRequestRecord keywordRecord = CrawlerRequestRecord.builder()
                .startPageRequest("dcdapp_series_keyword",turnPageItem)
                .httpUrl("http://192.168.1.217:9599/v1/meta/dcdapp/keys?site=series")
                .requestLabelTag(supportSource)
                .requestLabelTag(internalDownload)
                .build();

        return CrawlerJob.builder()
                .crawlerJobThreadNumber(10)
                .triggerInfo(
                        domain,
                        CrawlerMetaConstant.ScheduleJobTrigger_Cron,
                        System.currentTimeMillis(),//dcdapp-praise-koubei-series-everyDay-realtime
                        StringUtils.joinWith("-",site,site_biz,site_Info,CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime))
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(StringUtils.joinWith("-","crawler",domain,site,site_Info,site_biz,crawler_level,"queue")))
                .kafkaResultPipeline("kafka",kafkaTopicForRealTimeJob,null)
                .requestRecord(crawlerRequestRecord)
                .supportRecord(keywordRecord)
                .build();
    }


    /**
     * 问答板块需要的参数
     * @param crawlerRequestRecord
     */
    private static void wenda(CrawlerRequestRecord crawlerRequestRecord){
        List<String> moduleTag = new ArrayList<>();//不同板块
        moduleTag.add("wenda");//问答板块
        crawlerRequestRecord.tagsCreator().bizTags().addCustomKV("moduleTag",moduleTag);
        List<String> moduleSort = new ArrayList<>();
        moduleSort.add("1");//问答排序方式 1代表最新发布  2代表最新回复
        crawlerRequestRecord.tagsCreator().bizTags().addCustomKV("moduleSort",moduleSort);
    }
    /**
     * 动态板块需要的参数
     * @param crawlerRequestRecord
     */
    private static void dongtai(CrawlerRequestRecord crawlerRequestRecord){
        List<String> moduleTag = new ArrayList<>();//不同板块
        moduleTag.add("dongtai");//动态板块
        crawlerRequestRecord.tagsCreator().bizTags().addCustomKV("moduleTag",moduleTag);
        List<String> moduleSort = new ArrayList<>();
        moduleSort.add("1");//问答排序方式 1代表最新发布  2代表最新回复
        crawlerRequestRecord.tagsCreator().bizTags().addCustomKV("moduleSort",moduleSort);
    }
    /**
     * 精华板块需要的参数
     * @param crawlerRequestRecord
     */
    private static void jinghua(CrawlerRequestRecord crawlerRequestRecord){
        List<String> moduleTag = new ArrayList<>();//不同板块
        moduleTag.add("selected");//精华板块
        crawlerRequestRecord.tagsCreator().bizTags().addCustomKV("moduleTag",moduleTag);
        List<String> moduleSort = new ArrayList<>();
        moduleSort.add("1");//问答排序方式 1代表最新发布  2代表最新回复
        crawlerRequestRecord.tagsCreator().bizTags().addCustomKV("moduleSort",moduleSort);
    }
}
